<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.youdi365.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  心云间、凝听 <981248356@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Builder\AdminFormBuilder;
use Admin\Builder\AdminListBuilder;
class TermsController extends AdminController {
    protected $termsModel;
    protected $term_taxonomy;
    function _initialize()
    {
        parent::_initialize();
        $this->termsModel = D('terms');
        $this->term_taxonomy=dbC('TERM_TAXONOMY');//获取所有分类法
    }
		//分类管理
    public function index(){
    	$this->meta_title ='分类管理';
        // 获取所有用户
        $map['status'] = array('egt', '0','1'); // 发布和不发布状态
        $paged=I('get.p/d',1);
        list($data_list,$totalCount) = $this->termsModel->getListByPage($map,$paged,'sort desc,create_time desc','*',20);
        // 使用Builder快速建立列表页面。
        $builder = new AdminListBuilder();
        $builder->setMetaTitle('分类管理') // 设置页面标题
                ->addTopButton('addnew')  // 添加新增按钮
                ->addTopButton('resume')  // 添加启用按钮
                ->addTopButton('forbid')  // 添加禁用按钮
                ->addTopButton('recycle') //添加回收按钮
                ->setSearch('输入分类名称', U('index'))
                ->keyListItem('name', '名称','link',U('edit',array('term_id'=>'__data_id__')))
                ->keyListItem('slug', '别名')
                ->keyListItem('taxonomy', '类型','array',$this->term_taxonomy)
                ->keyListItem('term_id', '文章数','callback', array($this->termsModel,'term_relation_count'))
                ->keyListItem('status', '状态', 'status')
                ->keyListItem('right_button', '操作', 'btn')
                ->setListDataKey('term_id')
                ->setListData($data_list)    // 数据列表
                ->setListPage($totalCount,20) // 数据列表分页
                ->addRightButton('edit')// 添加编辑按钮
                ->addRightButton('recycle')// 添加删除按钮
                ->display();

    }

		//编辑
    public function edit($term_id=0){
        $title=$term_id?"编辑":"新增";
        if (IS_POST) {
            // 提交数据
            $data = $this->termsModel->create();
            if ($data) {
                $result = $this->termsModel->editData($data,'term_id');
                if ($result) {
                    $this->success($title.'成功', U('index'));
                } else {
                    $this->error($title.'失败', $this->termsModel->getError());
                }
            } else {
                $this->error($this->termsModel->getError());
            }
        } else {
            if ($term_id!=0) {
                $info = $this->termsModel->find($term_id);
            }
            // 使用FormBuilder快速建立表单页面。
            $builder = new AdminFormBuilder();
            $builder->setMetaTitle($title.'分类')  // 设置页面标题
            ->addFormItem('term_id', 'hidden', 'ID', 'ID')
            ->addFormItem('name', 'text', '分类名称', '分类名称')
            ->addFormItem('slug', 'text', '分类别名', '分类别名')
            ->addFormItem('taxonomy', 'select', '分类类型', '选择一个分类法',$this->term_taxonomy)
            ->setFormData($info)
            ->addButton('submit')->addButton('back')    // 设置表单按钮
            ->display();
        }
    }


}